package com.syqc.monitor.ui.cost

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.syqc.adapter.ChoosePhotoAdapter
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentRepairBinding
import com.syqc.monitor.databinding.IncludeDescPhotosBinding
import com.syqc.monitor.databinding.IncludeRecyclerWrapBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.InputUtil

class RepairFragment : BaseFeeFragment<FragmentRepairBinding>(FragmentRepairBinding::inflate) {

    private lateinit var descBinding: IncludeDescPhotosBinding
    private lateinit var mRecyclerVB: IncludeRecyclerWrapBinding
    private lateinit var sureBinding: IncludeSelectSureBinding
    private val mAdapter by lazy { ChoosePhotoAdapter() }
    private val mVM by viewModels<RepairViewModel>()
    override val mViewModel: BaseFeeViewModel get() = mVM
    override val isOil: Boolean
        get() = false
    override fun setDate(type: Int, time: String) {
        binding.lrvDate.setRightText(time)
        mVM.update = time
    }

    override fun uploadData() {
        mVM.uploadRepair()
    }

    override fun clearData() {
        binding.uetWork.setText("")
        binding.uetManage.setText("")
        binding.uetParts.setText("")
        binding.uetOutside.setText("")
        binding.uetOther.setText("")
        binding.uetPerson.setText("")
        binding.uetDriver.setText("")
        binding.uetTotal.setText("0.00")
        descBinding.editFeeRemark.setText("")
        mVM.clearData()
        binding.lrvDate.setRightText(mVM.update)
        binding.nsView.fullScroll(View.FOCUS_UP)
    }

    override fun initView(view: View) {
        descBinding = IncludeDescPhotosBinding.bind(binding.root)
        sureBinding = IncludeSelectSureBinding.bind(binding.root)
        sureBinding.tvSure.setText(R.string.desc_com_sure)
        focusChange(binding.uetWork.getEditText())
        focusChange(binding.uetManage.getEditText())
        focusChange(binding.uetParts.getEditText())
        focusChange(binding.uetOutside.getEditText())
        focusChange(binding.uetOther.getEditText())
        binding.uetTotal.getEditText().isEnabled=false
        binding.uetDriver.setFilter(15)
        binding.uetPerson.setFilter(15)
        InputUtil.editTextNested(descBinding.editFeeRemark)

        binding.lrvDate.setRightText(mVM.update)

        mRecyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
        mRecyclerVB.recyclerWrap.layoutManager = GridLayoutManager(requireContext(), 3)
        mRecyclerVB.recyclerWrap.adapter = mAdapter
    }

    override fun initData() {
        super.initData()
        mVM.handlePhotos(null)

        mVM.feeTotalLive.observe(this) {
            binding.uetTotal.setText(it)
        }

        mVM.photoLive.observe(this) {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initEvent() {
        binding.lrvDate.setOnClickListener {
            mDatePicker.show(mVM.update)
        }

        binding.uetWork.setOnTextChange {
            val str = isNumOk(binding.uetWork, it)
            mVM.feeWork = str
        }

        binding.uetManage.setOnTextChange {
            val str = isNumOk(binding.uetManage, it)
            mVM.feeManage = str
        }

        binding.uetParts.setOnTextChange {
            val str = isNumOk(binding.uetParts, it)
            mVM.feeParts = str
        }

        binding.uetOutside.setOnTextChange {
            val str = isNumOk(binding.uetOutside, it)
            mVM.feeOutSide = str
        }

        binding.uetOther.setOnTextChange {
            val str = isNumOk(binding.uetOther, it)
            mVM.feeOther = str
        }

        binding.uetPerson.setOnTextChange {
            mVM.person = it
        }

        binding.uetDriver.setOnTextChange {
            mVM.driver = it
        }

        sureBinding.tvSure.filterFast{
            mVM.uploadRepair()
        }

        descBinding.editFeeRemark.doAfterChanged {
            mVM.remark = it
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val path = mAdapter.data[position]
            if (path.isEmpty()) {
                takePhoto()
            }
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            mViewModel.removePhoto(position)
        }

    }

}